package leetcode;

/**
 * @Project : AlgorithmLearning
 * @Package : Leetcode
 * @File : Q0072.java
 * @Author : WangRuoyu
 * @Date 2023/11/27 21:10
 */

public class Q0072 {
    public int minDistance(String word1, String word2) {
        int[][] f = new int[word1.length() + 1][word2.length() + 1];
        for (int i = 0; i <= word1.length(); i++) f[i][0] = i;
        for (int i = 0; i <= word2.length(); i++) f[0][i] = i;

        for (int i = 1; i <= word1.length(); i++) {
            for (int j = 1; j <= word2.length(); j++) {
                f[i][j] = Math.min(f[i - 1][j], f[i][j - 1]) + 1;
                if (word1.charAt(i - 1) == word2.charAt(j - 1)) {
                    f[i][j] = Math.min(f[i][j], f[i - 1][j - 1]);
                } else {
                    f[i][j] = Math.min(f[i][j], f[i - 1][j - 1] + 1);
                }
            }
        }
        return f[word1.length()][word2.length()];
    }

    public static void main(String[] args) {
        Q0072 sl = new Q0072();
    }
}